//
//  CompanyGoodsViewController.m
//  ShangPiBao
//
//  Created by chirui on 2017/10/19.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "CompanyGoodsViewController.h"
#import "CompanyGoodsCVCell.h"
#import "GoodsDetailsViewController.h"

@interface CompanyGoodsViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    
    UICollectionView * _collectionView;
    
    NSMutableArray * _dataArray;
    int _page;
    int _totailPage;
}


@end

@implementation CompanyGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    _page = 1;
    
    _dataArray = @[].mutableCopy;
    
    
    [self creatCollectionView];
    [self getDataForRequest];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma creatCollectionView

-(void)creatCollectionView {
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-235) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    _collectionView.showsVerticalScrollIndicator = NO;
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _page = 1;
        [self getDataForRequest];
        
        
    }];
    _collectionView.mj_header = header;
    
    
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getDataForRequest];
    }];
    
    footer.automaticallyRefresh = YES;
    _collectionView.mj_footer =footer;
    _collectionView.mj_footer.hidden =YES;
    //注册cell类
    [_collectionView registerNib:[UINib nibWithNibName:@"CompanyGoodsCVCell" bundle:nil] forCellWithReuseIdentifier:@"CompanyGoodsCVCell"];
    
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return _dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dic = _dataArray[indexPath.row];
    
    CompanyGoodsCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CompanyGoodsCVCell" forIndexPath:indexPath];
    [cell.goodsImage sd_setImageWithURL:ImageUrl(dic[@"ppicture"]) placeholderImage:ImageName(@"img_home_newest")];
    cell.goodsName.text  = dic[@"ptitle"];
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];

  
     GoodsDetailsViewController * goods =[[GoodsDetailsViewController alloc]init];
      goods.goodsid = [NSString stringWithFormat:@"%@",dic[@"pid"]];
     [self.navigationController pushViewController:goods animated:YES];
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (ISP6P) {
        
        CGSize size=CGSizeMake((WIDTH-43)/3, (WIDTH-43)/3*1.15);
        
        return size;
    }else {
    
       CGSize size=CGSizeMake((WIDTH-40)/3, (WIDTH-40)/3*1.15);
    
    return size;
    }
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    //上 左 下 右
    return UIEdgeInsetsMake(0, 10, 0,10);
}

//设置水平间隙   默认10
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

////设置垂直间隙  默认10
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(0,10);
}
//设置footer的宽度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    //第一个参数：水平滑动时footer的宽度
    //第二个参数：垂直滑动时footer的高度
    return CGSizeMake(0, 0);
}

-(void)getDataForRequest{
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_collectionView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSString * md5ver = [SHJudgeTools md532BitLower:[NSString
                                                     stringWithFormat:@"%@api.shangpibao.com",USER_DEFAULTGet(@"phone")]];
    
    NSString * parametersStr = [NSString stringWithFormat:@"%@%@?uphone=%@&ver=%@&uid=%@&page=%d&versions=new",address_url,get_companyGoods_api,USER_DEFAULTGet(@"phone"),md5ver,self.companyId,_page];
    
    NSString *encoded = [parametersStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSLog(@"parametersStr = %@",parametersStr);
    //    SHuiMBhubNoTimeLoding;
    [PostNetWorking netWorkingStr:encoded  Success:^(id responseObject) {
        SHuiMBHidleYes;
        if ([responseObject[@"err"] intValue]==0) {
            
            _totailPage = [responseObject[@"maxpage"] intValue];
            
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"data"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"data"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }else {
                
                if ([responseObject[@"data"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"data"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                if (_dataArray.count>10) {
                    
                    _collectionView.mj_footer.hidden = NO;
                }else {
                    _collectionView.mj_footer.hidden = YES;
                    
                }
                [_collectionView.mj_header endRefreshing];
                [_collectionView.mj_footer endRefreshing];
                [_collectionView reloadData];
                
            });
            
        } else {
            
            SHuiMBhudTextTimeStr(EorrMessageStr);
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [_collectionView.mj_header endRefreshing];
            [_collectionView reloadData];
            
        });
        
        [_collectionView reloadData];
        
    } failed:^(NSError *error) {
        
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"请检查您的网络!");
    }];
    
}


@end
